//
//  GMCommentCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/19.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMCommentCell.h"
#import "GMlevelView.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "GMAttachListModel.h"
#import "GMLookPDFVC.h"
@interface GMCommentCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>
/**评论视图的高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHight;
/**评论人头像名*/
@property (weak, nonatomic) IBOutlet UIImageView *userHeadImg;
/**评论人姓名*/
@property (weak, nonatomic) IBOutlet UILabel *userName;
/**评论时间*/
@property (weak, nonatomic) IBOutlet UILabel *firstTime;
@property (weak, nonatomic) IBOutlet GMlevelView *levelView;
/**评论内容*/
@property (weak, nonatomic) IBOutlet UILabel *content;
/**评论语音*/
@property (weak, nonatomic) IBOutlet UITableView *voiceView;

/**评论语音视图的高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *voiceHight;
/**评论图片*/
@property (weak, nonatomic) IBOutlet UICollectionView *ImgsView;
/**评论图片的高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgsHight;
/**评论星星*/
@property (weak, nonatomic) IBOutlet XHStarRateView *statView;

/**点赞数*/
@property (weak, nonatomic) IBOutlet UILabel *likeCount;
@property (weak, nonatomic) IBOutlet UIButton *likeBtn;
/**点赞视图*/
@property (weak, nonatomic) IBOutlet UIView *likeView;

/**回复信息的总高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *repViewHight;
/**回复视图*/
@property (weak, nonatomic) IBOutlet UIView *repView;
/**回复人头像*/
@property (weak, nonatomic) IBOutlet UIImageView *repImg;
/**回复人姓名*/
@property (weak, nonatomic) IBOutlet UILabel *repName;
/**回复时间*/
@property (weak, nonatomic) IBOutlet UILabel *repTime;
/**回复内容*/
@property (weak, nonatomic) IBOutlet UILabel *repContent;
/**回复语音视图*/
@property (weak, nonatomic) IBOutlet UITableView *repVoiceView;
@property (weak, nonatomic) IBOutlet UICollectionView *repImgs;

/**回复语音高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *repVoiceHight;
/**回复的图片*/

/**回复的图片高度*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *repImgsHight;
@end
@implementation GMCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _ImgsView.dataSource = self;
    _ImgsView.scrollEnabled = NO;
    _ImgsView.delegate = self;
    [_ImgsView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    _voiceView.dataSource = self;
    _voiceView.delegate = self;
    _voiceView.scrollEnabled = NO;
    [_voiceView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    _statView.userInteractionEnabled = NO;
    _repVoiceView.dataSource = self;
    _repVoiceView.delegate = self;
    [_repVoiceView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    
    _repImgs.dataSource = self;
    _repImgs.scrollEnabled = NO;
    _repImgs.delegate = self;
    [_repImgs registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    
    //点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapChange:)];
    [self.likeView addGestureRecognizer:tap];
    
}
-(void)setReModel:(GMRemarksModel *)reModel{
    _reModel = reModel;
    [self.userHeadImg yy_setImageWithURL:[NSURL URLWithString:reModel.RemarkUserInfo.HeadUrl] placeholder:DefealtImg];
    self.userName.text = reModel.RemarkUserInfo.UserName;
    self.firstTime.text = reModel.RemarkTime;
    self.content.text = reModel.Remark;
    self.statView.currentRating = reModel.Score/2;
    [self.levelView setCrownCount:reModel.RemarkUserInfo.GradeIconCount.CrownCount SunCount:reModel.RemarkUserInfo.GradeIconCount.SunCount MoonCount:reModel.RemarkUserInfo.GradeIconCount.MoonCount StarCount:reModel.RemarkUserInfo.GradeIconCount.StarCount];
    self.likeCount.text =[NSString stringWithFormat:@"%ld",reModel.LikeCount];
    if (reModel.IsLike) {
        self.likeBtn.selected = YES;
    }else{
        self.likeBtn.selected = NO;
    }
    CGFloat contentHight = [self.content getLableSizeWithMaxWidth:(DEVICE_WIDTH-88)].height;
    if (reModel.VoiceList.count ==0) {
        self.voiceView.hidden = YES;
        self.voiceHight.constant = 0;
    }else{
        self.voiceView.hidden = NO;
        self.voiceHight.constant = 30*reModel.VoiceList.count;
        [self.voiceView reloadData];
    }
    
    if (reModel.ImgFileList.count ==0) {
        self.ImgsView.hidden = YES;
        self.imgsHight.constant =0;
    }else{
        self.ImgsView.hidden = NO;
        self.imgsHight.constant =60;
        [self.ImgsView reloadData];
    }
    self.contentViewHight.constant =60+2+contentHight+2+self.voiceHight.constant +2 +self.imgsHight.constant +2 +20+8;
    if (kIsEmptyObject(reModel.ReplyInfo)) {
        self.repVoiceView.hidden =YES;
        self.repVoiceHight.constant =0;
        self.repImgs.hidden = YES;
        self.repImgsHight.constant = 0;
        self.repView.hidden =YES;
        self.repViewHight.constant = 0;
    }else{
        self.repView.hidden =NO;
        [self.repImg yy_setImageWithURL:[NSURL URLWithString:reModel.ReplyInfo.HeadUrl] placeholder:DefealtImg];
        self.repName.text = reModel.ReplyInfo.UserName;
        self.repTime.text =reModel.ReplyInfo.ReplyTime;
        self.repContent.text =reModel.ReplyInfo.ReplyBody;
        CGFloat repContentHight = [self.repContent getLableSizeWithMaxWidth:(DEVICE_WIDTH-150)].height;
        if (reModel.ReplyInfo.VoiceList.count ==0) {
            self.repVoiceView.hidden =YES;
            self.repVoiceHight.constant =0;
        }else{
            self.repVoiceView.hidden =NO;
            self.repVoiceHight.constant =30 *reModel.ReplyInfo.VoiceList.count;
            [self.repVoiceView reloadData];
        }
        if (reModel.ReplyInfo.ImgFileList.count == 0) {
            self.repImgs.hidden = YES;
            self.repImgsHight.constant = 0;
        }else{
            self.repImgs.hidden = NO;
            self.repImgsHight.constant = 60;
            [self.repImgs reloadData];
        }
        self.repViewHight.constant = 40 +2 +repContentHight +2+self.repVoiceHight.constant +2+self.repImgsHight.constant;
    }
    reModel.cellHight = self.contentViewHight.constant +self.repViewHight.constant+8;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.repImgs]) {
         return self.reModel.ReplyInfo.ImgFileList.count;
    }else{
         return self.reModel.ImgFileList.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual:self.repImgs]) {
        GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
        GMAttachListModel *model= self.reModel.ReplyInfo.ImgFileList[indexPath.row];
        NSLog(@"%@",model.FileType);
        if ([model.FileType isEqualToNumber:@(3)]) {
            [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
        }else if ([model.FileType isEqualToNumber:@(4)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
        }else if ([model.FileType isEqualToNumber:@(5)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
        }else if ([model.FileType isEqualToNumber:@(6)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
        }else if ([model.FileType isEqualToNumber:@(7)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
        }else if ([model.FileType isEqualToNumber:@(8)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
        }else if ([model.FileType isEqualToNumber:@(9)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
        }else{
            cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
        }
        cell.delectBtn.hidden=YES;
        return cell;
    }else{
        GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
        GMAttachListModel *model= self.reModel.ImgFileList[indexPath.row];
        NSLog(@"%@",model.FileType);
        if ([model.FileType isEqualToNumber:@(3)]) {
            [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
        }else if ([model.FileType isEqualToNumber:@(4)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
        }else if ([model.FileType isEqualToNumber:@(5)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
        }else if ([model.FileType isEqualToNumber:@(6)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
        }else if ([model.FileType isEqualToNumber:@(7)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
        }else if ([model.FileType isEqualToNumber:@(8)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
        }else if ([model.FileType isEqualToNumber:@(9)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
        }else{
            cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
        }
        cell.delectBtn.hidden=YES;
        return cell;
    }
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.repImgs]) {
        return CGSizeMake((DEVICE_WIDTH-88-10-10-40-60)/5, (DEVICE_WIDTH-88-10-10-40-60)/5);
    }else{
        return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
    }
 
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([collectionView isEqual:self.repImgs]) {
        /**放大图片*/
        GMAttachListModel *fileModel= self.reModel.ReplyInfo.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.reModel.ReplyInfo.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.reModel.ReplyInfo.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.baseVC];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.baseVC.navigationController pushViewController:preview animated:YES];
        }
    }else{
        /**放大图片*/
        GMAttachListModel *fileModel= self.reModel.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.reModel.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.reModel.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.baseVC];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.baseVC.navigationController pushViewController:preview animated:YES];
        }
    }
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.repVoiceView]) {
        return self.reModel.ReplyInfo.VoiceList.count;
    }else{
        return self.reModel.VoiceList.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      if ([tableView isEqual:self.repVoiceView]) {
          GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
          GMVoiceListModel *model = self.reModel.ReplyInfo.VoiceList[indexPath.row];
          cell.model = model;
          kWeakObject(self)
          cell.readBlock = ^{
              weakself.hasReadBlock(indexPath.row, model.Id);
          };
          return cell;
      }else{
          GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
          GMVoiceListModel *model = self.reModel.VoiceList[indexPath.row];
          cell.model = model;
          kWeakObject(self)
          cell.readBlock = ^{
              weakself.hasReadBlock(indexPath.row, model.Id);
          };
          return cell;
      }
   
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(void)doTapChange:(id)tap{
    self.likeBlock();
}

@end
